/***********************************************
 **** 版权声明处 **
 ****  为了方便阅读和维护，请严格遵守相关代码规范，谢谢   ****
 *******************************************/
package bpm.graphic
{
	import bpm.graphic.decorators.DecoratorPlayer;
	import bpm.graphic.skins.LinkAdornerSkin;
	import bpm.graphic.skins.SkinBase;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;

	/*******************************************
	 **** huangjixin, 2012-10-31, 下午3:28:09 **
	 **** 请一句话表述该类主要作用  **
	 *******************************************/
	public class LinkAdorner extends ResizeAdorner
	{
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 类私有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：private static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 类公有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：public static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 私有变量声明处，请以“_”开头定义变量
		// 例如：private var _example:String;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 公有变量声明处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		[SkinPart]
		public var startHandle:Handle;
		[SkinPart]
		public var endHandle:Handle;
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 构造函数，初始化相关工作可以放在里面
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		public function LinkAdorner(adornedObject:Render)
		{
			super(adornedObject);
			setStyle("skinClass",LinkAdornerSkin);
		}//构造函数结束
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// getter和setter函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 相关事件响应函数和逻辑函数存放处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// override 覆盖函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		override protected function handlePressed(handle:DisplayObject, event:MouseEvent):void{
			pressHandle = handle as Handle;
			/*this._startPoint.x = mouseX;
			this._startPoint.y = mouseY;
			this._startPoint  = this.localToGlobal(_startPoint);
			
			_rect.x = this._startPoint.x;
			_rect.y = this._startPoint.y;
			_rect.width = adornedObject.width;
			_rect.height = adornedObject.height;*/
			
			FlexGlobals.topLevelApplication.parent.addEventListener(MouseEvent.MOUSE_MOVE,onHandleMouseMove);
			FlexGlobals.topLevelApplication.parent.addEventListener(MouseEvent.MOUSE_UP,onHandlerMouseUp);
		}
		
		override protected function onHandleMouseMove(event:MouseEvent):void
		{
			var point:Point = new Point(mouseX,mouseY);
			point = localToGlobal(point);
			point = adornedObject.globalToLocal(point);
			this.adornedObject.graphics.clear();
			this.adornedObject.graphics.lineStyle(2,0,0.5);
			if(pressHandle == startHandle){
//				this.adornedObject.graphics.moveTo(0,0);
				this.adornedObject.graphics.moveTo(Link(this.adornedObject).fallbackEndPoint.x,Link(this.adornedObject).fallbackEndPoint.y);
			}else if(pressHandle == endHandle){
				this.adornedObject.graphics.moveTo(Link(this.adornedObject).fallbackStartPoint.x,Link(this.adornedObject).fallbackStartPoint.y);
			}
			this.adornedObject.graphics.lineTo(point.x,point.y);
		}
		
		override protected function onHandlerMouseUp(event:Event):void
		{
			this.adornedObject.graphics.clear();
			var point:Point = new Point(mouseX,mouseY);
			point = localToGlobal(point);
			point = adornedObject.globalToLocal(point);
			
			if(pressHandle == startHandle){
				Link(this.adornedObject).fallbackStartPoint = point;
			}else if(pressHandle == endHandle){
				Link(this.adornedObject).fallbackEndPoint = point;
			}
			var object:Object = event.target;
			var render:Render;
			var index:int;
			var hostComponent:UIComponent;
			var resizeAdorner:ResizeAdorner;
			
			if(object is Port){
				if(pressHandle == startHandle){
					Link(this.adornedObject).startPort = object as Port;
					
					if(Link(this.adornedObject).startNode){//先干掉原来连接；
						if(Link(this.adornedObject).startNode is Render){
							render = Link(this.adornedObject).startNode as Render;
						}
						
						if(render.links.contains(this.adornedObject)){
							index = render.links.getItemIndex(this.adornedObject);
							render.links.removeItemAt(index);
						}
					}
					
					if(object.parentDocument.hasOwnProperty("hostComponent")){
						hostComponent = object.parentDocument["hostComponent"];
						
						if(hostComponent is ResizeAdorner){ //建立起双向关联；
							resizeAdorner = hostComponent as ResizeAdorner;
							resizeAdorner.adornedObject.links.addItem(Link(this.adornedObject));
							Link(this.adornedObject).startNode = resizeAdorner.adornedObject;
						}
					}
				}else if(pressHandle == endHandle){
					Link(this.adornedObject).endPort = object as Port;
					
					if(Link(this.adornedObject).endNode){
						if(Link(this.adornedObject).endNode is Render){
							render = Link(this.adornedObject).endNode as Render;
						}
						
						if(render.links.contains(this.adornedObject)){
							index = render.links.getItemIndex(this.adornedObject);
							render.links.removeItemAt(index);
						}
					}
					
					if(object.parentDocument.hasOwnProperty("hostComponent")){
						hostComponent = object.parentDocument["hostComponent"];
						
						if(hostComponent is ResizeAdorner){ //建立起双向关联；
							resizeAdorner = hostComponent as ResizeAdorner;
							resizeAdorner.adornedObject.links.addItem(Link(this.adornedObject));
							Link(this.adornedObject).endNode = resizeAdorner.adornedObject;
						}
					}
				}
				
				Alert.show("你的鼠标已经放在了端口上面，先给你记着");
			}else if(object is Render && !object is Link){
				Alert.show("你的鼠标已经放在了Render上面");
			}else{
				if(pressHandle == startHandle){
					if(Link(this.adornedObject).startNode){
						if(Link(this.adornedObject).startNode is Render){
							render = Link(this.adornedObject).startNode as Render;
						}
						
						if(render.links.contains(this.adornedObject)){
							index = render.links.getItemIndex(this.adornedObject);
							render.links.removeItemAt(index);
						}
					}
					
					Link(this.adornedObject).startPort = null;
				}else if(pressHandle == endHandle){
					if(Link(this.adornedObject).endNode){
						if(Link(this.adornedObject).endNode is Render){
							render = Link(this.adornedObject).endNode as Render;
						}
						
						if(render.links.contains(this.adornedObject)){
							index = render.links.getItemIndex(this.adornedObject);
							render.links.removeItemAt(index);
						}
					}
					Link(this.adornedObject).endPort = null;
				}
			}
			
			super.onHandlerMouseUp(event);
			
//			FlexGlobals.topLevelApplication.parent.removeEventListener(MouseEvent.MOUSE_MOVE,onHandleMouseMove);
//			FlexGlobals.topLevelApplication.parent.removeEventListener(MouseEvent.MOUSE_UP,onHandlerMouseUp);
		}
		
		override protected function partRemoved(partName:String, instance:Object):void{
			super.partRemoved(partName,instance);
			
			if(instance == startHandle){
				startHandle.removeEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
			if(instance == endHandle){
				endHandle.removeEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
		}
		
		override protected function partAdded(partName:String, instance:Object):void{
			super.partAdded(partName,instance);
			
			if(instance == startHandle){
				startHandle.addEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
			if(instance == endHandle){
				endHandle.addEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
		}
	}//类结束
}//包结束